/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.q1cc.cfs.jamp.players;

import com.sun.jna.NativeLibrary;
import net.q1cc.cfs.jamp.Main;
import net.q1cc.cfs.jamp.event.EventType;
import net.q1cc.cfs.jamp.event.JAmpAbstractEvent;
import net.q1cc.cfs.jamp.event.JAmpEvent;
import net.q1cc.cfs.jamp.event.JAmpSongEvent;
import net.q1cc.cfs.jamp.schema.Song;
import uk.co.caprica.vlcj.player.MediaPlayer;
import uk.co.caprica.vlcj.player.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;

/**
 *
 * @author claus
 */
public class VLCPlayerHandler implements PlayerHandler {
    
    MediaPlayerFactory factory;
    MediaPlayer mediaPlayer;
    Status status;
    Song song;
    
    @Override
    public void destroy() {
        mediaPlayer.stop();
        factory.release();
        status=Status.STOPPED;
    }

    @Override
    public void initPlayer() {
        NativeLibrary.addSearchPath("vlc", "/usr/lib");
        NativeLibrary.addSearchPath("vlc", System.getenv("ProgramFiles")+"\\VideoLAN\\VLC");
        
        System.out.println(System.getenv("ProgramFiles")+"\\VideoLAN\\VLC");
        System.setProperty("vlcj.check", "no");
        
        factory = new MediaPlayerFactory();
        mediaPlayer = factory.newHeadlessMediaPlayer();
        mediaPlayer.addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
            @Override
            public void finished(MediaPlayer mediaPlayer) {
                status=Status.ENDED;
                Main.getInstance().getEventReporter().throwEvent(new JAmpSongEvent("song start", null, JAmpSongEvent.SONG_ENDED, null));
            }
            @Override
            public void error(MediaPlayer mediaPlayer) {
                status=Status.ERROR;
            }
            @Override public void playing(MediaPlayer mplayer) {
                status=Status.PLAYING;
                Main.getInstance().getEventReporter().throwEvent(new JAmpSongEvent("song start", null, JAmpSongEvent.SONG_STARTED, song));
            }
        });
        status=Status.INIT;
        
    }

    @Override
    public void play() {
        mediaPlayer.play();
        status=status.STARTED;
    }

    @Override
    public void playSong(Song song) {
        this.song=song;
        playFile(song.getValue("URL"));
        
    }

    @Override
    public void playFile(String file) {
        mediaPlayer.playMedia(file);
        System.out.println(file);
        status=Status.STARTED;
    }

    @Override
    public void playpause() {
        mediaPlayer.pause();
        if(status==Status.PLAYING){
            status=Status.PAUSED;
        } else if(status==Status.PAUSED){
            status=Status.PLAYING;
        } else {
            status=Status.ERROR;
        }
    }

    @Override
    public void seek(long timeInMs) {
        mediaPlayer.setTime(timeInMs);
    }

    @Override
    public Status getStatus() {
        return status;
    }

    @Override
    public void stop() {
        mediaPlayer.stop();
        status=Status.STOPPED;
    }

    @Override
    public long getPosition() {
        return mediaPlayer.getTime();
    }

    @Override
    public long getDuration() {
        return mediaPlayer.getLength();
    }
    
}
